package com.qf.service.impl;

import com.qf.dto.ContractorDto;
import com.qf.mapper.ContractorMapper;
import com.qf.mapper.ProjectMapper;
import com.qf.model.Contractor;
import com.qf.model.Project;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.ContractorService;
import com.qf.utils.UUIDUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ContractorServiceImpl implements ContractorService {
    @Autowired
    private ContractorMapper contractorMapper;
    @Autowired
    private ProjectMapper projectMapper;
    //分页查询+模糊查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        //当前页结果
        List<Contractor> rows = contractorMapper.findPage((CurrentPage-1)*PageSize, PageSize,queryString);
        //总记录数
        Long count = contractorMapper.count(queryString);
        pageResult.setTotal(count);
        pageResult.setRows(rows);
        return pageResult;
    }

    //新建开发商并绑定工程
    @Override
    @Transactional
    public boolean add(String cname,int[] pids) {
        Contractor contractor = new Contractor();
        contractor.setCid(UUIDUtils.uuid());
        contractor.setCname(cname);
        contractor.setUpdatetime(new Date());
        //新增开发商
        int i = contractorMapper.add(contractor);
        //绑定工程
        for (int pid : pids) {
            int i1 = projectMapper.bindContractor(pid, contractor.getCid());
            if (i1 <=0 ){
                return false;
            }
        }
        if (i>0){
            return true;
        }else {
            return false;
        }
    }


    //根据cid查询ContractorDto
    @Override
    public ContractorDto findById(String cid) {

        ContractorDto contractorDto = contractorMapper.findById(cid);
        List<Project> projects = projectMapper.findByCid(cid);
        int[] pids = null;
        if (ObjectUtils.isNotEmpty(projects)){
            pids= new int[projects.size()];
            for (int i = 0; i < projects.size(); i++) {
                pids[i] = projects.get(i).getPid();
            }
        }

        contractorDto.setPids(pids);
        return contractorDto;
    }

    //修改contractor
    @Override
    @Transactional
    public boolean edit(String cname,int[] pids,String cid) {
        Contractor contractor = new Contractor();
        contractor.setCname(cname);
        contractor.setCid(cid);
        contractor.setUpdatetime(new Date());
        int i = contractorMapper.editOne(contractor);
        //取消所有绑定了对应cid的project的cid
        projectMapper.cancelBindContractorByCid(cid);
//        List<Project> projects = projectMapper.findByCid(cid);
//        for (Project project : projects) {
//            if (!ArrayUtils.contains(pids,project.getPid())){
//                //pids中不包含就取消绑定
//                int i1 = projectMapper.cancelBindContractor(project.getPid());
//            }
//        }
        //修改绑定工程
        for (int pid : pids) {
            int i1 = projectMapper.bindContractor(pid, cid);
            if (i1<=0){
                return false;
            }
        }
        if (i>0){
            return true;
        }
        return false;
    }

    //删除contractor
    @Override
    @Transactional
    public int delete(String cid) {
        int i = contractorMapper.delete(cid);
        if (i>0){
            int i1 = projectMapper.cancelBindContractorByCid(cid);
            if (i1 >0){
                return i1;
            }
        }
        return 0;
    }
}
